package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理类")
public class DishController {

    @Autowired
    private DishService dishService;

    @ApiOperation("分页查询菜品信息")
    @GetMapping("/page")
    public Result getDishPage(DishPageQueryDTO dto) {
        PageResult pageResult = dishService.getDishPage(dto);
        return Result.success(pageResult);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public Result getDishById(@PathVariable Long id) {

        //TODO 注意解决口味选择有两组的情况，会返回两条
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    //分两步走，第一步先新增菜品，然后返回该表主键，然后再新增在口味表中
    @ApiOperation("新增菜品")
    @PostMapping
    @CacheEvict(cacheNames = "dishCache", key = "#dto.categoryId")
    public Result saveDishWithFlavor(@RequestBody  DishDTO dto) {
        dishService.saveDishWithFlavor(dto);
        return Result.success();
    }

    //删除菜品
    @ApiOperation("删除菜品")
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache" ,allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        dishService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("修改菜品信息")
    @PutMapping
    @CacheEvict(cacheNames = "dishCache" ,allEntries = true)
    public Result updateDishWithFlavor(@RequestBody DishDTO dto) { //看清接口要求
        dishService.updateDishWithFlavor(dto);
        return Result.success();
    }

    @ApiOperation("修改菜品状态信息")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache" ,allEntries = true)
    public Result updateDishStatus(@PathVariable Integer status,Long id) {
        dishService.updateDishStatus(status,id);
        return Result.success();
    }

    @ApiOperation("根据类别查找查找菜品")
    @GetMapping("/list")
    public Result getDishByCategoryId(Integer categoryId) {
        List<Dish> list = dishService.getDishByCategoryId(categoryId);
        return Result.success(list);
    }
}
